package com.lotusfall.controller;

import com.github.pagehelper.PageInfo;
import com.lotusfall.config.UserSecurity;
import com.lotusfall.pojo.HouseResource;
import com.lotusfall.service.PicUploadService;
import com.lotusfall.service.UserService;
import com.lotusfall.pojo.User;
import com.lotusfall.vo.PicUploadResult;
import com.lotusfall.vo.WebResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private PicUploadService picUploadService;

    /**
     * 查询所有用户信息（管理员权限）
     * @return
     */
    @RequestMapping("/user/queryAll")
    @ResponseBody
    @PreAuthorize("hasRole('管理员')")
    public List<User> queryAll(){
        return userService.queryAll();
    }


//    @RequestMapping("/user/getUserInfo")
//    @GetMapping
//    @ResponseBody
//    public WebResult getUserInfo(Principal principal){
//        String username = principal.getName();
//        String
//        return WebResult.ok(list);
//    }

    /**
     * 分页查询用户（管理员权限）
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping("/user/queryUserList")
    @PostMapping
    @ResponseBody
    @PreAuthorize("hasRole('管理员')")
    public WebResult queryUserList(Integer page,Integer pageSize){
        List<PageInfo<User>> result = new ArrayList<>();
        result.add(userService.queryUserList(page,pageSize));
        return WebResult.ok(result);
    }

    /**
     * 获取当前用户信息
     * @return
     */
    @RequestMapping("/user/getCurrentUser")
    @ResponseBody
    public WebResult getCurrentUser(){
        UserSecurity userDetails =  (UserSecurity)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        String imageUrl = userDetails.getLoginUser().getAvatar();
        List<Object> userInfo = new ArrayList<>();
        userInfo.add(username);
        userInfo.add(imageUrl);
        return WebResult.ok(userInfo);
    }


    /**
     * 创建新用户
     * @param username
     * @param password
     * @param email
     * @param tel
     * @param sex
     * @param avatar
     * @return
     */
    @RequestMapping("/user/addUser")
    @PostMapping
    @ResponseBody
    public WebResult addUser(String username, String password, String email, String tel, boolean sex, MultipartFile avatar){
        User user = new User();

        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setTel(tel);
        user.setSex(sex);

        if(this.userService.addUser(user)){
            PicUploadResult picUploadResult = this.picUploadService.upload(avatar);
            userService.setUserAvatar(username,picUploadResult.getName());
        }
        return WebResult.ok(new ArrayList<>());
    }


    /**
     * 更改当前用户头像
     * @param avatar
     * @return
     */
    @RequestMapping("/user/setUserAvatar")
    @PostMapping
    @ResponseBody
    public WebResult setUserAvatar(MultipartFile avatar){
        PicUploadResult picUploadResult = this.picUploadService.upload(avatar);
        userService.setUserAvatar(SecurityContextHolder.getContext().getAuthentication().getName(),picUploadResult.getName());
        return WebResult.ok(new ArrayList<>());
    }

}
